/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mrroman.linksender.filesender.server;

import com.mrroman.linksender.filesender.sendables.Sendable;
import java.io.File;
import java.net.Socket;
import java.util.logging.Logger;

/**
 *
 * @author gorladam
 */
public class ServerThreadFactory {

    private String serverName = ServerThreadFactory.class.getSimpleName();
    private String serverHomepage = "/";
    private String outputDirectory = "";
    private String menu = null;
    private Logger logger = Logger.getLogger(ServerThreadFactory.class.getName());
    final String headBodyHTTP;

    public ServerThreadFactory(String serverName, String serverHomepage, String outputDirectory) {
        if (serverName != null) {
            this.serverName = serverName;
        }
        if (serverHomepage != null) {
            this.serverHomepage = serverHomepage;
        }
        if (outputDirectory != null) {
            File od = new File(outputDirectory);
            if (od.isDirectory() && od.canWrite()) {
                this.outputDirectory = outputDirectory;
            }
        }
        headBodyHTTP = HttpTools.convertToMultiline(new String[]{
                    HttpTools.XHTMLHEADER,
                    "<head>",
                    "<title>" + this.serverName + "</title>",
                    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />",
                    HttpTools.getDefaultCSS(),
                    "</head>",
                    "<body>",});
    }

    public String getServerHomepage() {
        return serverHomepage;
    }

    public String getServerName() {
        return serverName;
    }

    public String getOutputDirectory() {
        return outputDirectory;
    }

    public void setMenu(String... hrefs) {
        if (hrefs == null || hrefs.length == 0) {
            menu = null;
            return;
        }
        StringBuilder buffer = new StringBuilder(hrefs.length * (10 + hrefs[0].length()));
        buffer.append("<div id=\"MENU\"><ul>" + "\r\n");
        for (String href : hrefs) {
            buffer.append("  <li>");
            buffer.append(href);
            buffer.append("</li>" + "\r\n");
        }
        buffer.append("</ul></div>");
        menu = buffer.toString();
    }

    public String getHeadBodyHTTP() {
        return headBodyHTTP;
    }

    public String getMenu() {
        return menu;
    }

    public Runnable getServerRunnable(final Socket socket_, final Sendable root) {
        return new ServerThread(this, socket_, root);
    }
}
